package com.guobox.movies.present;

import com.guobox.movies.entity.MovieEntity;
import com.guobox.movies.entity.MovieType;
import com.guobox.movies.usecase.MoviesModel;
import com.guobox.movies.usecase.MoviesModelImp;
import com.guobox.movies.usecase.UpdateModel;
import com.guobox.movies.usecase.UpdateModelImp;
import com.guobox.movies.view.MovieActivityIf;

import java.io.FileOutputStream;
import java.util.List;

/**
 * Created by xuxinlei on 2017/6/30.
 */

public class MoviesListPresentImp implements MovieListPresent {

    private MovieActivityIf movieActivityIf;
    private MoviesModel moviesModel;
    private UpdateModel updateModel;

    public MoviesListPresentImp(MovieActivityIf movieActivityIf) {
        this.movieActivityIf = movieActivityIf;
        moviesModel = new MoviesModelImp(this);
        updateModel = new UpdateModelImp(this);
    }


    @Override
    public void getMoviesRequest(int movieTypeCode,String sort,String ip) {

        moviesModel.getMoviesFromNas(MovieType.getMovieTypes().get(movieTypeCode).getTypeName(),sort,ip);
    }

    @Override
    public void moviesResultCallback(List<MovieEntity> movieEntities, String movieTypeName) {
        movieActivityIf.refreshView(movieEntities, movieTypeName);
    }

    @Override
    public void checkAppUpdateCallBack(boolean needUpdate, String desc) {
        movieActivityIf.checkUpdateAppCallback(needUpdate,desc);
    }

    @Override
    public void checkAppUpdate() {
        updateModel.checkAppUpdate();
    }

    @Override
    public void downloadAppResult(boolean isDownloaded) {
        movieActivityIf.downloadAppResult(isDownloaded);
    }

    @Override
    public void downloadAppProgress(int progress) {
        movieActivityIf.downloadProgress(progress);
    }

    @Override
    public void updateAppRequest() {
        updateModel.downloadApp();
    }

    @Override
    public void delApk() {
        updateModel.delApk();
    }


    //往SD卡写入文件的方法
    public void savaFileToSD(String filename, String filecontent) throws Exception {
        //如果手机已插入sd卡,且app具有读写sd卡的权限

        //这里就不要用openFileOutput了,那个是往手机内存中写数据的
        FileOutputStream output = new FileOutputStream(filename);
        output.write(filecontent.getBytes());
        //将String字符串以字节流的形式写入到输出流中
        output.close();
        //关闭输出流
    }
}
